package com.example.administrator.myapplication.net.chain;

import android.util.Log;

import com.example.administrator.myapplication.net.Call;
import com.example.administrator.myapplication.net.HttpClient;
import com.example.administrator.myapplication.net.Response;

public class RetryInterceptor implements Interceptor {
    @Override
    public Response intercept(InterceptorChain chain) throws Exception {
        Log.e("RetryInterceptor","重试拦截器");
        Call call = chain.call;
        HttpClient client = call.client();
        Exception exception = null;
        for (int i = 0; i < client.retrys(); i++) {
            if (call.isCanceled()) {
                throw new IllegalStateException("request was canceled");
            }
            try {
                Response response = chain.process();
                return response;
            } catch (Exception e) {
                exception = e;
            }
        }
        throw exception;
    }
}
